<template>
	<view>
<!-- 		<view v-if="current==0" style="padding: 0 20rpx;">
			<u--input class="search" placeholder="Search for form" border="false" v-model="queryParams.search" @change="search"></u--input>
		</view> -->
		
		<u-tabs :list="tabList" :is-scroll="false" :current="current" @change="change" style="width:100% ;"></u-tabs>

		<view class="content" v-if="current==0">
			<view class="" v-for="(item, index) in formList" :key="index">
				<ListItem :title="item.describe_info" :details="item.describe_info" link
					@click.native="updateJump(item)">
					<template #photo>
						<image src="../../static/icon-font/MockTest.png" mode="" style="width: 84rpx;height: 84rpx;">
						</image>
					</template>
				</ListItem>
			</view>
		</view>
		<view class="content" v-if="current==1">
			<view class="" v-for="(item, index) in list" :key="index">
				<ListItem :title="item.name" :details="item.value" link @click.native="addJump(item.key)">
					<template #photo>
						<image src="../../static/icon-font/MockTest.png" mode="" style="width: 84rpx;height: 84rpx;">
						</image>
					</template>
				</ListItem>
			</view>
		</view>


	</view>
</template>

<script>
	import ListItem from '@/components/basicListItem/ShadowyListItem/ShadowyListItem.vue'
	import formApi from '@/api/forms.js'
	export default {
		components: {
			ListItem
		},
		// 底部上拉刷新
		onReachBottom() {
			if (this.lastPage > 1 && this.queryParams.page <= this.lastPage) {
				//判断是否还有数据需要加载
				uni.showLoading({
					title: 'Loading'
				});
				setTimeout(function() {
					uni.hideLoading();
				}, 2000);
				this.queryParams.page++
				this.getList()
			} else {
				uni.showToast({
					title: 'nomoreText',
					icon: 'none'
				});
			}
		},
		data() {
			return {
				tabList: [
					{name: 'My Form'},
					{name: 'Forms'}],
				current: 0,
				list: [
					{name: 'Feedback Form',key: 0,value: ''}, 
					{name: 'Resources Request Form',key: 1,value: ''},
					{name: 'Interview/Staffing Request Form',key: 2,value: ''}, 
					{name: 'Support',key: 3,value: ''}],
				userId: this.$store.state.userId,
				formList: [],
				queryParams: {
					page: 1,
					search:'',
				},
				lastPage: 1,
			}
		},
		onLoad() {
			this.getList()
		},
		methods: {
			// tab选择
			change(index) {
				this.current = index.index;
			},
			//搜索
			search(e){
				this.queryParams.page = 1; // 重置页码为1
				this.formList = []; // 清空数组
				this.getList();
			},
			getList() {
				Promise.all([
					formApi.getfeedBack(this.queryParams),
					formApi.getResources(this.queryParams),
					formApi.getStaffing(this.queryParams),
					// formApi.getSupport(this.queryParams)
				]).then(responses => {
					responses.forEach(response => {
						//获取最大的页码
						if(response.data.data.list.last_page > this.lastPage){
							this.lastPage = response.data.data.list.last_page;
						}
						
						const url = response.config.url; // 获取接口路径
						const parts = url.split(/[./]/); // 将路径按照 . 和 / 分隔成数组
						const type = parts[2]; // 提取所需的部分，这里假设路径的第三个部分是你想要的
						const addType = response.data.data.list.data.map(item => {
							return {
								...item,
								type: type
							};
						});
						this.formList.push(...addType);
					});
				});
			},
			addJump(e) {
				const pages = [
					'feedback', 'resources', 'staffing', 'support'
				]
				const url = `./components/fromDetail?type=${pages[e]}&update=`;
				uni.navigateTo({
					url: url
				});
			},
			updateJump(e) {
				//加密，JSON.stringify解析到空值报错
				const formList = encodeURIComponent(JSON.stringify(e))
				const url = `./components/fromDetail?type=${e.type}&formList=${formList}&update=${true}`;
				uni.navigateTo({
					url: url
				});
			},
		}
	}
</script>

<style>
.search {
		width: 700rpx;
		height: 66rpx;
		background: #FFFFFF;
		box-shadow: 0rpx 6rpx 12rpx 2rpx rgba(174, 174, 174, 0.16);
		border-radius: 100rpx;
		margin: 24rpx 0 ;
	}
</style>